/*
	socket data message 
	|1|0|0|1|t|y|p|e| | | | | | | | | | | | | | | | |......         JSON           ......|
	|   A   |   B   |        		C				|						D		     |
	A:开始标志位		占4位
	B:数据类型		占4位	1<type<15
	C:JSON消息总长度	占16位	
	D:JSON消息体		
	
	added by liyuan @2017 11 03
*/
#ifndef ___H_____MSG____H_____
#define ___H_____MSG____H_____ 

#ifndef NULL
#define NULL 0
#endif

#define MSG_MAX_LEN 1024
#define MSG_HEAD_LEN 4
#define IS_MSG(msg)		((msg)[0] ==0x09)
#define SET_MSG_HEAD(msg)	((msg)[0]=0x09)

#define GET_MSG_TYPE(msg) (msg)[1]
#define SET_MSG_TYPE(msg,type)	((msg)[1] = (type))
#define GET_MSG_LEN(msg) (*((short*)(msg+2)))
#define SET_MSG_LEN(msg,len) (*((short*)(msg+2)) = (short)len)

#define MSG_TYPE_CTL_CAR			0x01	/*小车控制消息*/
#define MSG_TYPE_RESULT				0x02
#define MSG_TYPE_USER_CMD			0x03	/*小车主人指令消息*/
#define MSG_TYPE_CTL_CAR_HAND		0x04	/*控制小车机械手消息*/


//MSG_WARN_CODE	0x00-0xFF

//MSG_ERROR_CODE 0x100-0x1FF
#define MSG_ERROR_TOO_LONG_MSG		0x100	
#define MSG_ERROR_NOT_A_MSG			0x101	
#define MSG_ERROR_NULL_MSG			0x102	

#define MSG_WARN_CODE	




//小车设备编号定义------device_number---------------------
#define DEVICE_ALL_WHEELS						0x0001
#define DEVICE_LEFT_WHEEL						0x0002
#define DEVICE_RIGHT_WHEEL						0x0003

#define DEVICE_HAND_MOTORS						0x100
//爪子
#define DEVICE_HAND_MOTOR_01					0x101
//A
#define DEVICE_HAND_MOTOR_02					0x102
//B
#define DEVICE_HAND_MOTOR_03					0x103
//底
#define DEVICE_HAND_MOTOR_04					0x104
//云台左右
#define DEVICE_HAND_MOTOR_05					0x105
//去台上下
#define DEVICE_HAND_MOTOR_06					0x106
//---------------------------------------------


//操作指令编号---------------------------------
#define CTL_CAR_CMD_MOVE_UP						0x0001
#define CTL_CAR_CMD_MOVE_DOWN					0x0002
#define CTL_CAR_CMD_MOVE_LEFT					0x0003
#define CTL_CAR_CMD_MOVE_RIGHT					0x0004
#define CTL_CAR_CMD_TURN_LEFT					0x0005
#define CTL_CAR_CMD_TURN_RIGHT					0x0006
#define CTL_CAR_CMD_FORWARD_LEFT				0x0007
#define CTL_CAR_CMD_FORWARD_RIGHT				0x0008
#define CTL_CAR_CMD_BACKWARD_LEFT				0x0009
#define CTL_CAR_CMD_BACKWARD_RIGHT				0x0010
#define CTL_CAR_CMD_STOP						0x0011

#define CTL_CAR_CMD_CTL_MOTION					0x0101		//舵机单动作控制
#define CTL_CAR_CMD_CTL_MOTION_GROUP			0x0102		//舵机动作组控制
//----------------------------------------------



typedef struct msg_{
	char type;
	int len; //data的长度，整个消息的长度等于len+3
	char* data;//[MSG_MAX_LEN];

} msg_base;

void * robot_malloc(int size,int reset);

void printfmsg(char* msg,int len);

int serialize_msg(msg_base * msg , char** out_msg , int * out_len);

int deserialize_msg(char * msg, int len , msg_base** out_msg);

#endif
